﻿using PdfChartHelper.Models;
using PdfChartHelper.Others;
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfChartHelper.Writer
{
    public class RadarChartWriter
    {
       
        public static void FiveChart(PdfDocument doc, XPoint coordinateOrigin, double radius, List<RadaModel> radas)
        {

            if (radius <= 0)
                throw new UnderZeroException("半径不可以小于0");



            PdfPage first = doc.Pages[0];

            XGraphics graphics = XGraphics.FromPdfPage(first);
            //画辅助坐标
            graphics.DrawLine(new XPen(XColors.Black, 1), new XPoint(0, coordinateOrigin.Y), new XPoint(2 * coordinateOrigin.X, coordinateOrigin.Y)); //X
            graphics.DrawLine(new XPen(XColors.Black, 1), new XPoint(coordinateOrigin.X, 0), new XPoint(coordinateOrigin.X, 2 * coordinateOrigin.Y)); //Y

            //内外全部圈的半径
            double[] quan = new double[]
            {
                radius,
                radius/5*4,
                radius/5*3,
                radius/5*2,
                radius/5
            };

            bool control_color = false;

            //画分界圈
            quan.ToList().ForEach(item => {
                XPoint[] points = new XPoint[]
                   {

                    new XPoint(){ X = coordinateOrigin.X, Y = coordinateOrigin.Y -item},
                    new XPoint(){ X = coordinateOrigin.X - Math.Sin((72*Math.PI)/180)*item, Y = coordinateOrigin.Y -Math.Sin((18*Math.PI)/180)*item },
                    new XPoint(){ X = coordinateOrigin.X - Math.Sin((36*Math.PI)/180)*item, Y = coordinateOrigin.Y + Math.Sin((54*Math.PI)/180)*item},
                    new XPoint(){ X = coordinateOrigin.X + Math.Sin((36*Math.PI)/180)*item , Y = coordinateOrigin.Y +Math.Sin((54*Math.PI)/180)*item },
                    new XPoint(){ X = coordinateOrigin.X + Math.Sin((72*Math.PI)/180)*item,Y = coordinateOrigin.Y - Math.Sin((18*Math.PI)/180)*item},
                    new XPoint(){ X = coordinateOrigin.X, Y = coordinateOrigin.Y - item}
                   };

                if (!control_color)
                {
                    //graphics.DrawLines(new XPen(XColors.RoyalBlue, 1), points);
                    graphics.DrawPolygon(new XPen(XColors.Black, 1), XBrushes.White, points, XFillMode.Alternate);
                    control_color = true;
                }
                else
                {
                    XBrush custom_color = new XSolidBrush(XColor.FromArgb(246, 248, 252));
                    graphics.DrawPolygon(new XPen(XColors.Black, 1), custom_color, points, XFillMode.Alternate);
                    control_color = false;
                }

                //连接正五边形的各个点


                //画坐标点到各个定点的连接线（外接圆半径）
                if (item.Equals(radius))
                {
                    List<string> descs = radas[0].RadaValues.Keys.ToList<string>();
                    graphics.DrawString(descs[0], new XFont("Arial", 6), XBrushes.Navy, coordinateOrigin.X - 10, coordinateOrigin.Y - radius - 10, XStringFormats.BaseLineCenter);

                    graphics.DrawString(descs[1], new XFont("Arial", 6), XBrushes.Navy,
                    (coordinateOrigin.X - Math.Sin((72 * Math.PI) / 180) * radius) - 10, coordinateOrigin.Y - Math.Sin((18 * Math.PI) / 180) * radius - 10,
                        XStringFormats.BaseLineCenter);

                    graphics.DrawString(descs[2], new XFont("Arial", 6), XBrushes.Navy,
                    (coordinateOrigin.X - Math.Sin((36 * Math.PI) / 180) * radius) - 10, coordinateOrigin.Y + Math.Sin((54 * Math.PI) / 180) * radius + 10,
                        XStringFormats.BaseLineCenter);

                    graphics.DrawString(descs[3], new XFont("Arial", 6), XBrushes.Navy,
                    (coordinateOrigin.X + Math.Sin((36 * Math.PI) / 180) * radius) + 10, coordinateOrigin.Y + Math.Sin((54 * Math.PI) / 180) * radius + 10,
                        XStringFormats.BaseLineCenter);

                    graphics.DrawString(descs[4], new XFont("Arial", 6), XBrushes.Navy,
                    (coordinateOrigin.X + Math.Sin((72 * Math.PI) / 180) * radius) + 10, coordinateOrigin.Y - Math.Sin((18 * Math.PI) / 180) * radius + 10,
                        XStringFormats.BaseLineCenter);
                }

            });

            #region 画坐标原点和顶点的连线，并标记顶点的说明值

            //画坐标原点和顶点的连线

            XColor linecolor = XColor.FromArgb(187, 187, 187);

            graphics.DrawLine(new XPen(linecolor, 0.3), new XPoint(coordinateOrigin.X, coordinateOrigin.Y),
                new XPoint(coordinateOrigin.X - Math.Sin((72 * Math.PI) / 180) * radius, coordinateOrigin.Y - Math.Sin((18 * Math.PI) / 180) * radius));
            graphics.DrawLine(new XPen(linecolor, 0.3), new XPoint(coordinateOrigin.X, coordinateOrigin.Y),
               new XPoint(coordinateOrigin.X - Math.Sin((36 * Math.PI) / 180) * radius, coordinateOrigin.Y + Math.Sin((54 * Math.PI) / 180) * radius));
            graphics.DrawLine(new XPen(linecolor, 0.3), new XPoint(coordinateOrigin.X, coordinateOrigin.Y),
               new XPoint(coordinateOrigin.X + Math.Sin((36 * Math.PI) / 180) * radius, coordinateOrigin.Y + Math.Sin((54 * Math.PI) / 180) * radius));
            graphics.DrawLine(new XPen(linecolor, 0.3), new XPoint(coordinateOrigin.X, coordinateOrigin.Y),
               new XPoint(coordinateOrigin.X + Math.Sin((72 * Math.PI) / 180) * radius, coordinateOrigin.Y - Math.Sin((18 * Math.PI) / 180) * radius));
            graphics.DrawLine(new XPen(linecolor, 0.3), new XPoint(coordinateOrigin.X, coordinateOrigin.Y),
                new XPoint(coordinateOrigin.X, coordinateOrigin.Y - radius));

            #endregion
            //循环外层 RadaModel
            radas.ForEach(item => {

                double max = item.RadaValues.Values.Max();
                double[] real_len = new double[5];


                for (int i = 0; i < item.RadaValues.Count; i++)
                {
                    real_len[i] = (item.RadaValues.Values.ToArray()[i] / max) * radius;
                }


                XPoint[] points = new XPoint[]
                   {

                        new XPoint(){ X = coordinateOrigin.X, Y = coordinateOrigin.Y -real_len[0]},
                        new XPoint(){ X = coordinateOrigin.X - Math.Sin((72*Math.PI)/180)*real_len[1], Y = coordinateOrigin.Y -Math.Sin((18*Math.PI)/180)*real_len[1]},
                        new XPoint(){ X = coordinateOrigin.X - Math.Sin((36*Math.PI)/180)*real_len[2], Y = coordinateOrigin.Y + Math.Sin((54*Math.PI)/180)*real_len[2]},
                        new XPoint(){ X = coordinateOrigin.X + Math.Sin((36*Math.PI)/180)*real_len[3], Y = coordinateOrigin.Y +Math.Sin((54*Math.PI)/180)*real_len[3]},
                        new XPoint(){ X = coordinateOrigin.X + Math.Sin((72*Math.PI)/180)*real_len[4],Y = coordinateOrigin.Y - Math.Sin((18*Math.PI)/180)*real_len[4]},
                        new XPoint(){ X = coordinateOrigin.X, Y = coordinateOrigin.Y - real_len[0]}
                   };

                graphics.DrawLines(new XPen(XColors.Red, 0.5), points);
                //graphics.DrawPolygon(new XPen(XColors.Red, 0.5), XBrushes.DarkSalmon, points, XFillMode.Alternate);


            });

        }

        public static void SixChart(PdfDocument doc, XPoint coordinateOrigin, double radius, List<RadaModel> radas)
        {
        }
    }
}
